﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bird.Services.Events
{
    public class EventPublisher : IEventPublisher
    {
        private readonly IEventSubcriptionService _subcriptionService;
        public EventPublisher(IEventSubcriptionService subscriptionService)
        {
            _subcriptionService = subscriptionService;
        }
        public void Publish<T>(T message)
        {
            _subcriptionService.GetEventWatchers<T>()
                .ToList()
                .ForEach(x => SendEventToWatcher(x, message));
        }
        private static void SendEventToWatcher<T>(IEventWatcher<T> watcher, T message)
        {
            watcher.HandleEvent(message);
        }
    }
}
